<?php

use Phalcon\Events\Event,
    Phalcon\Mvc\User\Plugin,
    Phalcon\Mvc\Dispatcher,
    Phalcon\Mvc\Model\Query,
    Phalcon\Acl;

/**
 * Security
 *
 * This is the security plugin which controls that users only have access to the modules they're assigned to
 */
class LogPlugin extends Plugin
{


    /**
     * This action is executed before execute any action in the application
     *
     * @param Event $event
     * @param Dispatcher $dispatcher
     */
    public function beforeDispatch(Event $event, Dispatcher $dispatcher)
    {

        $controller = $dispatcher->getControllerName();
        // $controller =  str_replace('_','',$controller);
        $action = $dispatcher->getActionName();
        $url = $this->request->get('_url',null,"/$controller/$action");
        $method = $this->request->getMethod();
        $data = '';
        switch($method){

            case 'POST': $data =json_encode($this->request->getPost());break;
            case 'PUT': $data = json_encode($this->request->getPut());break;
            case 'GET':
            default: ;
        }




        $this->dispatcherLogger->log("<tr><td>method:$method</td> <td>url:$url</td><td>userid:".$this->session->get('yh',array('yid'=>'anonymous'))['yid']."</td><td>$data</td></tr>", \Phalcon\Logger::NOTICE);
        if(filesize(APP_PATH."/app/logs/Dispatcher.log")>1024*1024*30){
            $time = time();
            copy(APP_PATH."/app/logs/Dispatcher.log",APP_PATH."/app/logs/Dispatcher$time.log");
            unlink(APP_PATH."/app/logs/Dispatcher.log");
        }

    }
}
